package com.mp3.player.views
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Timer;
	
	import mx.controls.Label;
	import mx.core.UIComponent;
	import mx.effects.Move;
	import mx.events.EffectEvent;
	
	import org.osmf.events.TimeEvent;
	
	/**
	 * 
	 * @author Admin
	 * Movable label class.
	 * If text is larger then component text will be moved from left to right and vice versa
	 */	
	public class MovableLabel extends Label
	{
		private var _mask: Sprite;
		
		private var _text: String;
		private var _textChanged: Boolean;
		
		private var _move: Move;
		private var _delay: int = 1000;
		private var _speedCoef: int = 50;
		
		private var _setToStart: Boolean = true;
		
		private var _widthChanged: Boolean = false;
		
		/**
		 * Constructor
		*/
		public function MovableLabel()
		{
			super();
			
			truncateToFit = false;
		}
		
		//--------------------------------------------------------------------------
		//
		//Override methods
		//
		//---------------------------------------------------------------------------
		override public function set width(value: Number): void
		{
			super.width = value;
			
			_widthChanged = true;
			invalidateProperties();
		}
		
		override protected function createChildren(): void
		{
			super.createChildren();
			
			_move = new Move();
			_move.target = textField;
			_move.startDelay = _delay;
			
			_mask = new Sprite();
			addChild(_mask);
			
			mask = _mask;
		}
		
		
		override protected function commitProperties(): void
		{
			super.commitProperties();
			
			if (_textChanged || _widthChanged)
			{
				_setToStart = true;
				
				//stop all currecnt effects
				stopAllEffects();
				
				_textChanged = false;
				_widthChanged = false;
			}
		}
		
		override protected function updateDisplayList(unscaledWidth: Number, unscaledHeight: Number): void 
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			_mask.graphics.clear();
			_mask.graphics.beginFill(0x000000);
			_mask.graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
			_mask.graphics.endFill();
			
			if (_setToStart)
			{
				textField.setActualSize(textField.getExplicitOrMeasuredWidth(), textField.getExplicitOrMeasuredHeight());
				
				if (textField.getExplicitOrMeasuredWidth() > unscaledWidth)
				{
					textField.move(0, 0);
					_move.duration = (textField.getExplicitOrMeasuredWidth() - unscaledWidth) * _speedCoef;
					runEffect();
				}
				else
				{
					textField.move((unscaledWidth - textField.getExplicitOrMeasuredWidth()) / 2, 0);
				}
				
				_setToStart = false;
			}
		}
		
		//--------------------------------------------------------------------------
		//
		//Properties
		//
		//---------------------------------------------------------------------------
		/**
		 * Set text methods
		 * @param value
		 */		
		override public function set text(value: String): void
		{
			super.text = value;
			_textChanged = true;
			
			invalidateProperties();
			invalidateSize();
			invalidateDisplayList();
		}
		
		//--------------------------------------------------------------------------
		//
		//Private
		//
		//---------------------------------------------------------------------------
		private function stopAllEffects(): void
		{
			_move.removeEventListener(EffectEvent.EFFECT_END, onEffectEnd);
			_move.stop();
		}
		
		public function runEffect(): void
		{
			_move.addEventListener(EffectEvent.EFFECT_END, onEffectEnd);
			onEffectEnd();
		}
		
		private function onEffectEnd(event: Event = null): void
		{
			_move.xTo = (textField.x < 0) ? 0 : width - textField.getExplicitOrMeasuredWidth();
			_move.play();
		}
	}
}